package com.scott.utils;


import com.github.pagehelper.PageHelper;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by taoy on 2017/2/6 0006.
 * 分页工具
 */
public class PageHelperUtil {

    /**
     * 开始分页
     *
     * @param params page:第几页
     *               size:每页几条数据
     */
    public static void startPage(Map<String, Object> params) {
        if (null != params) {
            Integer pageNum = (Integer) getParamsValue(params, "page");
            Integer pageSize = (Integer) getParamsValue(params, "size");
            if (null != pageNum && null != pageSize && pageSize > 0) {
                PageHelper.startPage(pageNum, pageSize);
            }
        }
    }

    /**
     * 根据键获取对应的值
     *
     * @param params 元对象
     * @param key    键
     * @return 值, 为Object
     */
    private static Object getParamsValue(Map<String, Object> params, String key) {
        return params.get(key);
    }

    /**
     * 将pageParams转换成Map
     *
     * @param params
     * @return
     */
    public static Map<String, Object> convertPageParams(PageParams params) {
        Map<String, Object> param = new HashMap<String, Object>();
        if (null != params) {
            param.put("page", params.getPage());
            param.put("size", params.getSize());
            param.putAll(params.getMapParams());
        }
        return param;
    }
}

